<?
/****************************************************************************************/

function Run($action, $params){
	//$params = getParams($params);
	return $action($params);
}
/****************************************************************************************/

function makeGlobals($params){
	foreach($params as $k=>$v){
		$GLOBALS[$k] = $v;
	}
}
/****************************************************************************************/

function getParams($params) {
	if($params){
		$tmp1 = explode('|', $params);
		foreach($tmp1 as $k => $v){
			$tmp2 = explode('::', $v);
			$tmp2[1] = unserialize($tmp2[1]);
			if(substr($tmp2[1],strlen($tmp2[1])-1) == '\\'){
				$tmp2[1] = substr($tmp2[1],0,strlen($tmp2[1])-1);
			}
			eval('$'.$tmp2[0]." = '".str_replace("'", "\'", $tmp2[1])."';");
		}
	}
	unset ($tmp1, $tmp2, $k, $v, $params);
	return get_defined_vars();
}
/****************************************************************************************/

function getData($data) {
	parse_str ($data, $returned);
	return $returned;
}
/****************************************************************************************/

function Show ($objResponse, $div, $style='visibility') {
	if($style == 'visibility'){
		$set_style = 'style.visibility';
		$status = 'visible';
	}elseif($style == 'display'){
		$set_style = 'style.display';
		$status = 'block';
	}
	$objResponse->addAssign($div, $set_style, $status);
}
/****************************************************************************************/

function Hide ($objResponse, $div, $style='visibility') {	
	if($style == 'visibility'){
		$set_style = 'style.visibility';
		$status = 'hidden';
	}elseif($style == 'display'){
		$set_style = 'style.display';
		$status = 'none';
	}
	$objResponse->addAssign($div, $set_style, $status);
}
/****************************************************************************************/

function debugObject($objResponse, $obj, $divId){
	ob_start();
	print_r($obj);
	$data = ob_get_contents();
	ob_end_clean();
	$objResponse->addAssign($divId, 'innerHTML', $data);
	//$objResponse->addScript("var timeoutID = setTimeout(\"document.getElementById('$divId').innerHTML='';\",15000);");
}
/****************************************************************************************/

function LoadPage ($objResponse, $destinationObject, $htmlObject, $type='htmlPage') {
	switch ($type){
		case 'htmlPage':
			$filename = $htmlObject;
			if(!is_file($filename)){
				return false;
			}

			ob_start();
			include_once($filename);
			$data = ob_get_contents();
			ob_end_clean();
		break;
		case 'URL':
			ob_start();
			include_once($htmlObject);
			$data = ob_get_contents();
			ob_end_clean();
		break;
		case 'htmlSource':
			$data = $htmlObject;
		break;
	}

	$objResponse->addAssign($destinationObject, 'innerHTML', $data);
}
/****************************************************************************************/

function errorMsg ($objResponse, $msg) {
	$objResponse->addAssign($GLOBALS['error_msg'], 'innerHTML', $msg);
	Hide(&$objResponse, $GLOBALS['notice_id'], 'display');
	Hide(&$objResponse, $GLOBALS['load_id']);
	Show(&$objResponse, $GLOBALS['error_id'], 'display');
}
/****************************************************************************************/

function okMsg ($objResponse, $msg) {
	$objResponse->addAssign($GLOBALS['ok_msg'], 'innerHTML', $msg);
	Hide(&$objResponse, $GLOBALS['error_id'], 'display');
	Hide(&$objResponse, $GLOBALS['load_id']);
	Show(&$objResponse, $GLOBALS['notice_id'], 'display');
}
/****************************************************************************************/

function myLoadPage($objResponse, $page, $params = array(), $set_params = '', $status = '', $msg = ''){
	if($params){
		makeGlobals($params);
	}

	LoadPage(&$objResponse, $GLOBALS['page_id'], $GLOBALS['path'].$page);

	if($set_params){
		$objResponse->addAssign($GLOBALS['params_id'], 'value', $set_params);
	}
	if($status == 'ok_msg'){
		errorMsg(&$objResponse, $msg);
	}elseif($status == 'error_msg'){
		okMsg(&$objResponse, $msg);
	}else{
		Hide(&$objResponse, $GLOBALS['error_id'], 'display');
		Hide(&$objResponse, $GLOBALS['notice_id'], 'display');
		Hide(&$objResponse, $GLOBALS['load_id']);
	}
}
/****************************************************************************************/

function openPage($page, $params){
	$objResponse = new xajaxResponse();
	$params_array = getData($params);
	if($params_array['set_params']){
		$set_params = $params;
	}else{
		$set_params = '';
	}

	if(in_array($params_array['status'], array('ok_msg', 'error_msg')) and $params_array['msg']){
		$status = $params_array['status'];
		$msg = $params_array['msg'];
	}else{
		$status = '';
		$msg = '';
	}

	//debugObject(&$objResponse, $params_array, 'debug');
	myLoadPage(&$objResponse, $page, $params_array, $set_params, $status, $msg);

	return $objResponse->getXML();
}
/****************************************************************************************/

function sgeneral($params){
	$objResponse = new xajaxResponse();
	$params_array = getData($params);
	Hide(&$objResponse, $GLOBALS['load_id']);
	myLoadPage(&$objResponse, 'general.html', $params_array);
	return $objResponse->getXML();
}
/****************************************************************************************/

function searchOrders($params){
	$objResponse = new xajaxResponse();
	$params_array = getData($params);
	$select_params = $params_array['select_params'];
	$options = $params_array['options'];
	$select_params['reseller_id'] = $GLOBALS['id'];
	$select_params['client_id'] = '^IS NOT NULL';
	$select_params['order_status'] = array('1', '2', '4', '8', '16', '32', '64', '1024');
	if(!$select_params['sum_amount_total']){
		$select_params['sum_amount_total'] = '^>0';
	}
	
	$options['group_by'] = array('order_id', 'sale_date', 'client_username','client_firstname', 'client_lastname', 'sum_amount_total', 'payment_method', 'payment_currency');
	$options['order_by'] = 'order_id';
	$options['order_by_'] = 'DESC';
	$result = $GLOBALS['selector']->get_data($select_params, $options, 'query');
	debugObject(&$objResponse, $result, $GLOBALS['debug_id']);
	return $objResponse->getXML();
}
/****************************************************************************************/

$xajaxRegisterFunctions = array (
	'openPage' => XAJAX_GET,
	'Run' => XAJAX_GET
)

?>